//
//  YDYLivePlayerController.m
//  DY_Bilibili
//
//  Created by 杨德勇 on 2016/10/24.
//  Copyright © 2016年 杨德勇. All rights reserved.
//

#import "YDYLivePlayerController.h"
#import <IJKMediaFramework/IJKMediaFramework.h>
#import <Masonry.h>
@interface YDYLivePlayerController ()
@property (weak, nonatomic) IBOutlet UIVisualEffectView *topBlurV;
@property (weak, nonatomic) IBOutlet UIVisualEffectView *buttomBlurV;
@property (weak, nonatomic) IBOutlet UIButton *playBtn;
/** player */
@property (nonatomic, strong) id<IJKMediaPlayback> player;
/** url */
@property (nonatomic, strong) NSURL *url;
/** title */
@property (nonatomic, copy) NSString *liveTitle;
@end

@implementation YDYLivePlayerController

+(instancetype)playLiveWithURL:(NSURL *)url title:(NSString *)title inController:(UIViewController *)controller{
    YDYLivePlayerController *player = [[YDYLivePlayerController alloc]initWithUrl:url title:title];
    YDYLog(@"%@",url);
    [controller presentViewController:player animated:YES completion:nil];
    
    return player;
}
-(instancetype)initWithUrl:(NSURL *)url title:(NSString *)title {
    if (self = [super init]) {
        self.url = url;
        self.title = title;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [IJKFFMoviePlayerController setLogReport:YES];
    [IJKFFMoviePlayerController setLogLevel:k_IJK_LOG_SILENT];
    
    if (!self.url){
        ydy_FailureInView(@"url为空", self.view);
        return;
    }
    
    [self setupPlayer];
    
}

-(void)setupPlayer{
    IJKFFOptions *options = [IJKFFOptions optionsByDefault];
   
    IJKFFMoviePlayerController *player = [[IJKFFMoviePlayerController alloc]initWithContentURL:self.url withOptions:options];
    player.scalingMode = IJKMPMovieScalingModeAspectFit;
    player.shouldAutoplay = YES;
    player.view.backgroundColor = [UIColor redColor];
   
    self.player = player;
    [player prepareToPlay];
    [self.view addSubview:player.view];
    [self.view sendSubviewToBack:player.view];
    
    [player.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    

    
}

- (IBAction)playBtnClick:(UIButton *)sender {
    sender.selected =  !sender.isSelected;
//    ydy_SuccessInView(@"aaaa", self.view);
    
    
}


#pragma mark -
#pragma mark -让屏幕旋转

//- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
//    
//    return UIInterfaceOrientationIsLandscape(toInterfaceOrientation);
//}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

@end
